﻿/*
 * @author: wizardc
 */

using System.Collections.Generic;
using Cysharp.Threading.Tasks;

namespace DouEditor.Resource
{
    /// <summary>
    /// 打包步骤执行器
    /// </summary>
    public class BuildStepExecutor
    {
        private Queue<BuildStepBase> _stepQueue = new Queue<BuildStepBase>();

        private string _error;
        
        /// <summary>
        /// 错误描述
        /// </summary>
        public string error => _error;
        
        public void AddStep(BuildStepBase buildStep)
        {
            _stepQueue.Enqueue(buildStep);
        }

        public async UniTask<bool> Execute(IBuildStepData data)
        {
            while (_stepQueue.Count > 0)
            {
                var step = _stepQueue.Dequeue();
                step.Init(data);
                if (!await step.Execute())
                {
                    _error = step.error;
                    return false;
                }
            }
            return true;
        }
    }
}
